<?php
/*
 * tiers/upload-photo.phtml
 */
$this->headTitle("Téléchargement d'une Photo");

$tiers = isset($tiers)&& $tiers? $tiers: null;
$idTiers =  $tiers ? (int) $tiers->id : 0;

$fichier = $this->fichier;
$dirPhoto = isset($this->dir_photo) ? str_replace('public/', '', $this->dir_photo) : null; 

?>

<div class="container-fluid">
    <div class="content-head">
        <h4>Téléchargement d'une Photo</h4>
    </div>
    <div class="content-body content-body-tiers-form">
            <?php if (is_null($fichier)): ?>                              
                <?php if ($this->error) : ?>       
                <div class="info"><?php echo $this->error; ?></div>
                <?php else: ?>            
                <?php endif; ?>
            <?php else: ?>
                <div class="photo-info">
                    <?php $basePath = str_replace('public', '', $this->basePath()); ?>
                    <img src="<?php echo $this->basePath() .  '/'. $dirPhoto . $this->destination ?>" width="200" height="250">
                </div>
                <div class="">
                    <span class="photo-warning">Vous devez-valider pour prendre en compte le fichier</span>
                    <button id="btn-valid" class="btn btn-info" style="float: right;" >Valider</button>
                </div>
                
            <?php endif; ?>
        
        <form id="tiers-photo-form" class="tiers-form well" action="<?php echo $this->url('RHX/default', array('controller' => 'tiers', 'action' => 'uploadPhoto' )) ?>" method="post" enctype="multipart/form-data" method='POST'>
            <input type="file" id="tiers-photo-file" name="photo" onchange="submit();" accept="image/jpeg,image/gif,image/png">
            <input type="hidden" name="tiers_id" value="<?php echo $idTiers; ?>">
        </form>
    </div>
</div>

<script type="text/javascript">
            $('#btn-valid').on('click', function(){
       <?php if ($idTiers!= 0 ): // editing ?>         
                // MAJ de la base de données
                ajax = $.ajax({
                    type: 'post',
                    url: '<?php echo $this->url('RHX/default', array('controller' => 'tiers', 'action' => 'editData')) ?>',
                    data: '&pk=<?php echo $idTiers ; ?>&name=photo&value=' + encodeURI('<?php echo $this->destination; ?>'),
                });
                // MAJ de l'interface
                ajax.done(function(data){
                    photoTiers = window.opener.document.getElementById('tiers-photo');
                    $photoTiers = $(photoTiers);
                    $photoTiers.attr('src', '<?php echo $this->basePath() .  '/' . $dirPhoto . $this->destination ?>');
                    window.close();
                });
                ajax.error(function(data){
                    console.log('MAJ error');
                });
        <?php else: // adding ?> 
            photoTiers = window.opener.document.getElementById('tiers-photo');
            $photoTiers = $(photoTiers);
            $photoTiers.attr('src', '<?php echo $this->basePath() .  '/' . $dirPhoto . $this->destination ?>');
            
            storePhotoTiers = window.opener.document.getElementById('store-photo');
            $storePhotoTiers = $(storePhotoTiers);
            $storePhotoTiers.val("<?php  echo $this->destination ; ?>");
            
            storePhotoTiersExt = window.opener.document.getElementById('store-photo_extension');
            $storePhotoTiersExt = $(storePhotoTiersExt);
            $storePhotoTiersExt.val ("<?php  echo $this->extension ; ?>");
            
            window.close();
        <?php endif; ?>           
        
            });
</script>
